package com.battery.web.controller.system.battery;

import java.math.BigDecimal;
import java.util.List;

import com.battery.common.enums.OnOffStatusEnum;
import com.battery.common.enums.PurposeEnum;
import com.battery.common.enums.YesOrNoEnum;
import com.battery.common.utils.DateUtils;
import com.battery.system.domain.BO.ItemsBO;
import com.battery.system.domain.VO.ItemsVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.battery.common.annotation.Log;
import com.battery.common.enums.BusinessType;
import com.battery.system.domain.Items;
import com.battery.system.service.battery.IItemsService;
import com.battery.common.core.controller.BaseController;
import com.battery.common.core.domain.AjaxResult;
import com.battery.common.utils.poi.ExcelUtil;
import com.battery.common.core.page.TableDataInfo;

/**
 * 商品Controller
 *
 * @author battery
 * @date 2021-01-25
 */
@Controller
@RequestMapping("/system/items")
public class ItemsController extends BaseController {


    private String prefix = "system/items";

    @Autowired
    private IItemsService itemsService;


    @Autowired
    private Sid sid;

    /**
     * 设备申领
     *
     * @return
     */
    @RequiresPermissions("system:items:devApplyView")
    @GetMapping("/devApply")
    public String devApply() {
        return prefix + "/devApply";
    }


    /**
     * 在线采购 - 商品列表
     *
     * @return
     */
    @RequiresPermissions("system:items:purchaseView")
    @GetMapping("/purchase")
    public String purchase() {
        return prefix + "/purchase";
    }


    /**
     * 积分兑换 - 商品列表
     *
     * @return
     */
    @RequiresPermissions("system:items:pointsExchange")
    @GetMapping("/pointsExchange")
    public String points_exchange() {
        return prefix + "/pointsExchange";
    }


    /**
     * 设备申领-商品
     *
     * @param itemsBO
     * @return
     */
    @RequiresPermissions("system:items:purchaseItemsList")
    @PostMapping("/purchaseItemsList")
    @ResponseBody
    public TableDataInfo purchaseItemsList(ItemsBO itemsBO) {
        startPage();
        List<ItemsVO> list = itemsService.purchaseItemsList(itemsBO);
        return getDataTable(list);
    }


    /**
     * 积分兑换 - 商品列表
     *
     * @param itemsBO
     * @return
     */
    @RequiresPermissions("system:items:pointsExchangeItemsList")
    @PostMapping("/pointsExchangeItemsList")
    @ResponseBody
    public TableDataInfo pointsExchangeItemsList(ItemsBO itemsBO) {
        startPage();
        List<ItemsVO> list = itemsService.pointsExchangeItemsList(itemsBO);
        return getDataTable(list);
    }

    /**
     * 设备申领-商品
     *
     * @param itemsBO
     * @return
     */
    @RequiresPermissions("system:items:devApplyItemsList")
    @PostMapping("/devApplyItemsList")
    @ResponseBody
    public TableDataInfo devApplyList(ItemsBO itemsBO) {
        startPage();
        List<ItemsVO> list = itemsService.devApplyItemsList(itemsBO);
        return getDataTable(list);
    }


    @RequiresPermissions("system:items:view")
    @GetMapping()
    public String items() {
        return prefix + "/items";
    }


    /**
     * 查询商品列表
     */
    @RequiresPermissions("system:items:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Items items) {
        startPage();
        List<Items> list = itemsService.selectItemsList(items);
        return getDataTable(list);
    }

    /**
     * 导出商品列表
     */
    @RequiresPermissions("system:items:export")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Items items) {
        List<Items> list = itemsService.selectItemsList(items);
        ExcelUtil<Items> util = new ExcelUtil<Items>(Items.class);
        return util.exportExcel(list, "items");
    }

    /**
     * 新增商品
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }


    /**
     * 添加 积分兑换 商品
     *
     * @return
     */
    @GetMapping("/addPointsExchange")
    public String addPointsExchange() {
        return prefix + "/addPointsExchange";
    }


    /**
     * 添加 在线采购 商品
     *
     * @return
     */
    @GetMapping("/addPurchase")
    public String addPurchase() {
        return prefix + "/addPurchase";
    }

    /**
     * 设备申领-商品添加
     *
     * @return
     */
    @GetMapping("/addDevApplyItem")
    public String addDevApplyItem() {
        return prefix + "/addDevApplyItem";
    }


    /**
     * 积分兑换 商品 添加
     *
     * @param items
     * @return
     */
    @RequiresPermissions("system:items:addPointsExchange")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping("/addPointsExchange")
    @ResponseBody
    public AjaxResult addPointsExchange(Items items) {

        if (StringUtils.isBlank(items.getItemName())) {
            return error("请填写商品名称");
        }
        // 校验商品名称是否重复
        Items itemsNameExist = itemsService.selectByNameAndPurpose(items.getItemName(), PurposeEnum.POINTS_EXCHANGE.getCode());
        if (itemsNameExist != null) {
            return error("商品名称重复，请重新输入");
        }

        if (StringUtils.isBlank(items.getImgUrl())) {
            return error("请上传商品照片");
        }

        if (items.getIntegral() == null) {
            return error("请填写积分！");
        }

        if (items.getIntegral() < 0) {
            return error("积分不能小于0");
        }

        if (items.getPostMan() == null) {
            return error("请填写邮费！");
        }

        if (items.getPostMan().compareTo(BigDecimal.ZERO) < 0) {
            return error("邮费不能小于0");
        }

        items.setPurpose(PurposeEnum.POINTS_EXCHANGE.getCode());
        items.setCreatedTime(DateUtils.getNowDate());
        items.setId(sid.nextShort());
        items.setOnOffStatus(OnOffStatusEnum.ON.getCode()); // 默认上线
        return toAjax(itemsService.insertSelective(items));
    }

    /**
     * 在线采购-商品添加
     *
     * @return
     */
    @RequiresPermissions("system:items:addPurchase")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping("/addPurchase")
    @ResponseBody
    public AjaxResult addPurchase(Items items) {

        if (StringUtils.isBlank(items.getItemName())) {
            return error("请填写商品名称");
        }
        // 校验商品名称是否重复
        Items itemsNameExist = itemsService.selectByNameAndPurpose(items.getItemName(), PurposeEnum.ONLINE_PURCHASE.getCode());
        if (itemsNameExist != null) {
            return error("商品名称重复，请重新输入");
        }

        if (StringUtils.isBlank(items.getImgUrl())) {
            return error("请上传商品照片");
        }


        if (items.getPrice() == null) {
            return error("请填写商品价格！");
        }

        if (items.getPrice().compareTo(BigDecimal.ZERO) < 0) {
            return error("商品价格不能小于0");
        }

        if (items.getPostMan() == null) {
            return error("请填写邮费！");
        }

        if (items.getPostMan().compareTo(BigDecimal.ZERO) < 0) {
            return error("邮费不能小于0");
        }

        items.setPurpose(PurposeEnum.ONLINE_PURCHASE.getCode());
        items.setCreatedTime(DateUtils.getNowDate());
        items.setId(sid.nextShort());
        items.setOnOffStatus(OnOffStatusEnum.ON.getCode()); // 默认上线
        return toAjax(itemsService.insertSelective(items));
    }

    /**
     * 设备申领-商品添加
     *
     * @param items
     * @return
     */
    @RequiresPermissions("system:items:addDevApplyItem")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping("/addDevApplyItem")
    @ResponseBody
    public AjaxResult addDevApplyItem(Items items) {

        if (StringUtils.isBlank(items.getItemName())) {
            return error("请填写商品名称");
        }
        // 校验商品名称是否重复
        Items itemsNameExist = itemsService.selectByNameAndPurpose(items.getItemName(), PurposeEnum.DEV_APPLICATION.getCode());
        if (itemsNameExist != null) {
            return error("商品名称重复，请重新输入");
        }

        if (items.getDevType() == null) {
            return error("请选择设备类型");
        }

        // 校验该设备类型是否已经选择
        Items itemDevTypeExist = itemsService.selectByDevTypeAndPurpose(items.getDevType(), PurposeEnum.DEV_APPLICATION.getCode());
        if (itemDevTypeExist != null) {
            return error("该设备类型已有商品");
        }

        if (StringUtils.isBlank(items.getImgUrl())) {
            return error("请上传商品照片");
        }
        items.setPurpose(PurposeEnum.DEV_APPLICATION.getCode());
        items.setCreatedTime(DateUtils.getNowDate());
        items.setId(sid.nextShort());
        items.setOnOffStatus(OnOffStatusEnum.ON.getCode()); // 默认上线
        return toAjax(itemsService.insertSelective(items));
    }


    /**
     * 新增保存商品
     */
    @RequiresPermissions("system:items:add")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Items items) {
        return toAjax(itemsService.insertItems(items));
    }

    /**
     * 修改商品
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        Items items = itemsService.selectItemsById(id);
        mmap.put("items", items);
        return prefix + "/edit";
    }


    /**
     * 采购商品 - 修改
     *
     * @param id
     * @param mmap
     * @return
     */
    @GetMapping("/editPurchasetem/{id}")
    public String editPurchasetem(@PathVariable("id") String id, ModelMap mmap) {
        Items items = itemsService.selectItemsById(id);
        mmap.put("items", items);
        return prefix + "/editPurchasetem";
    }


    /**
     * 修改保存商品
     */
    @RequiresPermissions("system:items:edit")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Items items) {
        return toAjax(itemsService.updateItems(items));
    }


    /**
     * 修改保存商品 (在线采购)
     */
    @RequiresPermissions("system:items:editPurchasetem")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PostMapping("/editPurchasetem")
    @ResponseBody
    public AjaxResult editPurchasetem(Items items) {

        if (StringUtils.isBlank(items.getId())) {
            return error("主键ID为空");
        }

        if (StringUtils.isBlank(items.getItemName())) {
            return error("请填写商品名称");
        }
        // 校验商品名称是否重复
        Items itemsNameExist = itemsService.selectByNameAndPurpose(items.getItemName(), PurposeEnum.ONLINE_PURCHASE.getCode());
        if (itemsNameExist != null && !itemsNameExist.getId().equals(items.getId())) {
            return error("商品名称重复，请重新输入");
        }


        if (StringUtils.isBlank(items.getImgUrl())) {
            return error("请上传商品照片");
        }

        if (items.getPrice() == null) {
            return error("请填写商品价格！");
        }

        if (items.getPrice().compareTo(BigDecimal.ZERO) < 0) {
            return error("商品价格不能小于0");
        }

        if (items.getPostMan() == null) {
            return error("请填写邮费！");
        }

        if (items.getPostMan().compareTo(BigDecimal.ZERO) < 0) {
            return error("邮费不能小于0");
        }


        items.setUpdatedTime(DateUtils.getNowDate());
        return toAjax(itemsService.updateByPrimaryKeySelective(items));
    }

    /**
     * 积分兑换 商品修改
     * editPointsExchange
     *
     * @param items
     * @return
     */
    @RequiresPermissions("system:items:editPointsExchange")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PostMapping("/editPointsExchange")
    @ResponseBody
    public AjaxResult editPointsExchange(Items items) {

        if (StringUtils.isBlank(items.getId())) {
            return error("主键ID为空");
        }

        if (StringUtils.isBlank(items.getItemName())) {
            return error("请填写商品名称");
        }
        // 校验商品名称是否重复
        Items itemsNameExist = itemsService.selectByNameAndPurpose(items.getItemName(), PurposeEnum.POINTS_EXCHANGE.getCode());
        if (itemsNameExist != null && !itemsNameExist.getId().equals(items.getId())) {
            return error("商品名称重复，请重新输入");
        }

        if (StringUtils.isBlank(items.getImgUrl())) {
            return error("请上传商品照片");
        }

        if (items.getIntegral() == null) {
            return error("请填写积分");
        }

        if (items.getIntegral() < 0) {
            return error("积分不能小于0");
        }

        if (items.getPostMan() == null) {
            return error("请填写邮费！");
        }

        if (items.getPostMan().compareTo(BigDecimal.ZERO) < 0) {
            return error("邮费不能小于0");
        }

        items.setUpdatedTime(DateUtils.getNowDate());
        return toAjax(itemsService.updateByPrimaryKeySelective(items));
    }

    /**
     * 删除商品
     */
    @RequiresPermissions("system:items:remove")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        if (StringUtils.isBlank(ids)) {
            return error("ids为空");
        }
        if (ids.contains(",")) {
            return error("暂时不支持批量操作");
        }
        Items itemsUpdate = new Items();
        itemsUpdate.setId(ids);
        itemsUpdate.setIsDelete(YesOrNoEnum.YES.getCode());
        return toAjax(itemsService.updateByPrimaryKeySelective(itemsUpdate));
    }


    /**
     * 编辑设备申领-商品
     *
     * @param id
     * @param mmap
     * @return
     */
    @GetMapping("/editDevApplyItem/{id}")
    public String editDevApplyItem(@PathVariable("id") String id, ModelMap mmap) {
        Items items = itemsService.selectItemsById(id);
        mmap.put("items", items);
        return prefix + "/editDevApplyItem";
    }


    /**
     * 编辑 积分兑换 -商品
     *
     * @param id
     * @param mmap
     * @return
     */
    @GetMapping("/editPointsExchange/{id}")
    public String editPointsExchange(@PathVariable("id") String id, ModelMap mmap) {
        Items items = itemsService.selectItemsById(id);
        mmap.put("items", items);
        return prefix + "/editPointsExchange";
    }


    /**
     * 修改保存商品
     */
    @RequiresPermissions("system:items:editDevApplyItem")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PostMapping("/editDevApplyItem")
    @ResponseBody
    public AjaxResult editDevApplyItem(Items items) {


        if (StringUtils.isBlank(items.getId())) {
            return error("主键ID为空");
        }

        if (StringUtils.isBlank(items.getItemName())) {
            return error("请填写商品名称");
        }
        // 校验商品名称是否重复
        Items itemsNameExist = itemsService.selectByNameAndPurpose(items.getItemName(), PurposeEnum.DEV_APPLICATION.getCode());
        if (itemsNameExist != null && !itemsNameExist.getId().equals(items.getId())) {
            return error("商品名称重复，请重新输入");
        }

        if (items.getDevType() == null) {
            return error("请选择设备类型");
        }

        // 校验该设备类型是否已经选择
        Items itemDevTypeExist = itemsService.selectByDevTypeAndPurpose(items.getDevType(), PurposeEnum.DEV_APPLICATION.getCode());
        if (itemDevTypeExist != null && !itemDevTypeExist.getId().equals(items.getId())) {
            return error("该设备类型已有商品");
        }

        if (StringUtils.isBlank(items.getImgUrl())) {
            return error("请上传商品照片");
        }
        items.setUpdatedTime(DateUtils.getNowDate());
        return toAjax(itemsService.updateByPrimaryKeySelective(items));
    }


    @RequiresPermissions("system:items:on_off_status")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PostMapping("/onOffItems")
    @ResponseBody
    public AjaxResult onOffItems(String ids, Integer on_off_status) {
        if (StringUtils.isBlank(ids)) {
            return error("未选择商品");
        }
        return toAjax(itemsService.onOffItems(ids, on_off_status));
    }


}
